﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using tracker.Models.DTO;
using tracker.Models.DAL;
using tracker.Models;

namespace tracker.Controllers
{
    public class HandlerGetDeviceInfoController : Controller
    {
        //
        // GET: /HandlerGetDeviceInfo/
        [CallAjaxFilter]
        public JsonStringResult Index(String DeviceID)
        {
            String result = "";
            List<ENTDeviceInfo> list = new List<ENTDeviceInfo>();
            if (!String.IsNullOrEmpty(DeviceID))
            {
                //if (DeviceID.Length == 33)
                //{
                //    DeviceID = DeviceID.Substring(1, 32);
                //}
                if (DeviceID.Length > 0)
                {
                    DeviceID = DeviceID.Substring(1, DeviceID.Length-1);
                }
                EDevice entDevice = new DeviceDAL().getByDeviceKeySecurity(DeviceID);
                
                if (entDevice != null)
                {                    
                    ENTDeviceInfo ent = new ENTDeviceInfo();
                    ent.GprsPassWord = entDevice.Password;
                    ent.GprsUserName = "";
                    ent.SensorId1 = "0";
                    ent.SN_IMEI_ID = entDevice.DeviceKey;
                    ent.TankSize = "0";
                    ent.TrackerActivated = "true";
                    ent.TrackerDaysRemain = "0";
                    ent.TrackerIconName = entDevice.PictureOfDevide;
                    ent.TrackerId = entDevice.DeviceID.ToString();
                    ent.TrackerName = entDevice.DeviceName;
                    ent.TrackerPassWord = entDevice.Password;
                    ent.TrackerRegisterTime = entDevice.CreateDate.ToString("yyyy-MM-dd hh:mm:ss tt");
                    ent.TrackerSimNumber = entDevice.SimNumber;
                    ent.TrackerType = new DeviceTypeDAL().getByID(entDevice.DeviceTypeID).DeviceTypeName;
                    // ent.TrackerPassWordValue = "*******";
                    list.Add(ent);
                    
                }
            }
            result = Utilities.getJsonString(list, "data:", 1);
            return new JsonStringResult(result); ;
        }

    }

    public class ENTDeviceInfo
    {
        public string SensorId1 { get; set; }
        public string TankSize { get; set; }
        public string TrackerId { get; set; }
        public string TrackerPassWord { get; set; }
        public string TrackerPassWordValue { get; set; }
        public string TrackerName { get; set; }
        public string TrackerSimNumber { get; set; }
        public string APN { get; set; }
        public string GprsUserName { get; set; }
        public string GprsPassWord { get; set; }
        public string TrackerRegisterTime { get; set; }
        public string TrackerActivated { get; set; }
        public string TrackerDaysRemain { get; set; }
        public string SN_IMEI_ID { get; set; }
        public string TrackerType { get; set; }
        public string TrackerIconName { get; set; }
        public string BaseMileage { get; set; }
    }
}
